package com.macro.mall.portal.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.macro.mall.common.api.CommonResult;
import local.com.quyum.aliyun.aliyunoss.service.OssService;
import local.com.quyum.aliyun.aliyunoss.service.VodService;
import local.com.quyum.aliyun.aliyunoss.util.ConstantVodUtils;
import local.com.quyum.aliyun.aliyunoss.util.InitVodCilent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 阿里云视频相关
 * 高强
 * @author 高强
 * @Date 2020年9月1日
 */
@Controller
@RequestMapping("/oss")
public class UmsAlyOSScontroller {

    @Autowired
    private OssService ossService;
    @Autowired
    private VodService vodService;

    /**
     * 图片上传接口
     * @param file 上传的图片
     * @return 图片的url
     */
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public CommonResult uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        //返回上传到oss的路径
        String url = ossService.uploadFileAvatar(file);
        return CommonResult.success(url);
    }

    //上传视频到阿里云
    @ResponseBody
    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    public CommonResult uploadAlyVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        return CommonResult.success(videoId);
    }

    //根据视频id删除阿里云视频
    @ResponseBody
    @RequestMapping(value = "/removeVideo", method = RequestMethod.POST)
    public CommonResult removeAlyVideo(@PathVariable String id) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return CommonResult.success("OK");
        }catch(Exception e) {
            e.printStackTrace();
//            throw new GuliException(20001,"删除视频失败");
            return CommonResult.failed();
        }
    }

    //删除多个阿里云视频的方法
    //参数多个视频id  List videoIdList
    @ResponseBody
    @RequestMapping(value = "/removeVideoList", method = RequestMethod.POST)
    public CommonResult deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return CommonResult.success("OK");
    }

    //根据视频id获取视频凭证
    @ResponseBody
    @RequestMapping(value = "/getVideoAuth", method = RequestMethod.GET)
    public CommonResult getPlayAuth(@RequestParam String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return CommonResult.success(playAuth);
        }catch(Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
